<?php

namespace app\api\controller\club;

use app\common\controller\ClubApi;

/**
 * 俱乐部小程序接口
 */
class Customer extends ClubApi
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * @api {post} /api/club/customer/login 客户登录
     * @apiGroup 客户
     *  
     * @apiParam {String} code 登录时获取的code,可通过wx.login获取
     * 
     */
    public function login($code='')
    {
        try{
            if(!$code){
                throw new \Exception('code 不能为空');
            }
            $data = $this->getService()->login($code);
            return $this->success('成功', $data);
        } catch (\think\exception\HttpResponseException $e){
            throw $e;
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
    
    /**
     * @api {get, post} /api/club/customer/profile 获取客户基本信息
     * @apiGroup 客户
     * 
     */
    public function profile()
    {
        try{
            $data  = [];
            $uid = $this->getService()->getLoginUid();
            $data['info'] = $this->getService()->getCustomerInfo($uid);
            return $this->success('成功', $data);
        } catch (\think\exception\HttpResponseException $e){
            throw $e;
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
    
    /**
     * @api {get, post} /api/club/customer/update 更新
     * @apiGroup 客户
     *
     * @apiParam {String} name 姓名
     * @apiParam {String} telephone 电话
     */
    public function update($name='',$telephone='')
    {
        try{
            $data  = [];
            $uid = $this->getService()->getLoginUid();
            $up = [];
            if($name){
                $up['name'] = $name;
            }
            if($telephone){
                $up['telephone'] = $telephone;
            }
            $this->getService()->updateCustomer($uid, $up);
            $data['info'] = $this->getService()->getCustomerInfo($uid);
            return $this->success('成功', $data);
        } catch (\think\exception\HttpResponseException $e){
            throw $e;
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
    
    /**
     * @api {get, post} /api/club/customer/orderlist 获取客户订单列表
     * @apiGroup 客户
     * 
     * @apiParam {String} status 状态
     *
     */
    public function orderlist($status='')
    {
        try{
            $data  = [];
            $uid = $this->getService()->getLoginUid();
            $data['list'] = $this->getService()->getCustomerOrderList($uid, $status);
            return $this->success('成功', $data);
        } catch (\think\exception\HttpResponseException $e){
            throw $e;
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
    
    /**
     * @api {get, post} /api/club/customer/orderdetail 获取客户订单详情
     * @apiGroup 客户
     *
     * @apiParam {String} order_id 订单ID
     */
    public function orderdetail($order_id)
    {
        try{
            if(!$order_id){
                throw new \Exception('订单ID不能为空');
            }
            $data  = [];
            $data['detail'] = $this->getService()->getOrderDetail($order_id);
            return $this->success('成功', $data);
        } catch (\think\exception\HttpResponseException $e){
            throw $e;
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
    
}
